package com.zte.cl.parser;

import java.util.Map;

import org.apache.log4j.Logger;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.selector.Html;

import com.zte.cl.model.News;
import com.zte.cl.service.PageContentProcessService;
import com.zte.cl.utils.SpringContextUtil;

/**
 * 内容页结果处理 Pipeline负责抽取结果的处理，包括计算、持久化到文件、数据库等。
 * 
 * @author Administrator
 * 
 */
public class ContentPagePipeline implements Pipeline {
	private final static Logger log = Logger.getLogger(ContentPagePipeline.class);

	private PageContentProcessService pageContentProcessService = (PageContentProcessService) SpringContextUtil.getBean("pageContentProcessServiceContentPageImpl");
	
	@Override
	public void process(ResultItems resultItems, Task task) {
		try {
			Map<String, Object> mapResult = resultItems.getAll();
			Html orig_page_html = (Html) mapResult.get("orig_page_html");// 原始网页
			
			News news = (News) mapResult.get("news");// 处理后的文章
			pageContentProcessService.processContent(news, orig_page_html);
		} catch (Exception e) {
			log.error("处理页面内容时发生错误!", e);
		}
	}
}
